package com.whmnx.main.ui.adapter;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.whmnx.base.bean.PingBean;
import com.whmnx.base.utils.ImageUtil;
import com.whmnx.base.utils.TimeUtils;
import com.whmnx.main.R;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author : huang
 * date   : 2020/10/19 0019 9:17
 * version: 1.0
 * @author Administrator
 */
public class PingJiaListAdapter extends CommonRecyclerAdapter<PingBean> {
    Context context;

    private OnSetClickListener onSendClickListener;


    public void setOnSendClickListener(OnSetClickListener onSendClickListener) {
        this.onSendClickListener = onSendClickListener;
    }

    public interface OnSetClickListener{
        void onSendClick(String text, String text2, String s);
    }

    boolean isAll;
    public PingJiaListAdapter(Context context) {
        super(context, R.layout.item_ping);
        this.context = context;
    }


    @Override
    public void onUpdate(BaseAdapterHelper helper, PingBean item, int position) {
        helper.setText(R.id.tv_Product_Name, (String) item.getUserName());
        helper.setText(R.id.tv_time, TimeUtils.getFriendlyTimeSpanByNow(item.getCreateDate()));

        helper.setText(R.id.tv_Product_SPEC, (String) item.getRemark_Content());

        ImageUtil.setImage((CircleImageView)helper.getView(R.id.iv_img), item.getUserImage());

        RecyclerView rlList =  helper.getView(R.id.rl_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        //配置布局，默认为vertical（垂直布局），下边这句将布局改为水平布局
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rlList.setLayoutManager(layoutManager);
        rlList.setItemAnimator(new DefaultItemAnimator());
        PingJiaListAdapter2  pingJiaListAdapter = new PingJiaListAdapter2(context);
        rlList.setAdapter(pingJiaListAdapter);
        pingJiaListAdapter.replaceAll(item.getChildListModel(), true);

        pingJiaListAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                if (onSendClickListener != null){
                    onSendClickListener.onSendClick(item.getChildListModel().get(i).getParentId(),item.getChildListModel().get(i).getRemark_Id(),item.getChildListModel().get(i).getUserName());
                }
            }
        });

        if (item.getChildListModel().size() == 0){
            helper.setVisible(R.id.tv_num, View.GONE);
        }else {
            helper.setText(R.id.tv_num, "展开" + item.getChildListModel().size() + "条回复");
        }

        helper.setOnClickListener(R.id.tv_num, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //全选
                if (!isAll) {
                    isAll = true;
                    rlList.setVisibility(View.GONE);
                } else {
                    isAll = false;
                    rlList.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
